﻿using System.Data;
using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using SqlExecHelper.SqlValue;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class ErrorCollectDAL : SqlBasicClass
        {
                public ErrorCollectDAL () : base ("ErrorCollect")
                {

                }

                public ErrorDatum[] Query (string code, IBasicPage paging, out long count)
                {
                        ISqlWhere where = null;
                        if (!code.IsNull ())
                        {
                                where = new LikeSqlWhere ("ErrorCode", 50, LikeQueryType.右) { Value = code };
                        }
                        return base.Query<ErrorDatum> ("Id desc", paging.Index, paging.Size, out count, where);
                }
                public ErrorDatum Get (long id)
                {
                        return base.GetRow<long, ErrorDatum> ("Id", id);
                }
                public long FindId (string code)
                {
                        return this.ExecuteScalar<long> ("Id", new SqlWhere ("ErrorCode", SqlDbType.VarChar, 50) { Value = code });
                }

                public void SetIsPerfect (long id)
                {
                        if (!this.Update (new ISqlSetColumn[] {
                                new SqlSetColumn("IsPerfect", SqlDbType.Bit){Value=1}
                        }, new ISqlWhere[] {
                                new SqlWhere("Id", SqlDbType.BigInt){Value=id}
                        }))
                        {
                                throw new ErrorException ("rpc.error.perfect.set.error");
                        }
                }

                public long Add (string errorCode)
                {
                        this.Insert (new IInsertSqlValue[] {
                                new InsertSqlValue("ErrorCode", SqlDbType.VarChar,50){ Value=errorCode},
                                new InsertSqlValue("IsPerfect", SqlDbType.Bit){Value=0}
                        }, out long id);
                        return id;
                }

                internal ErrorDatum[] Gets (long[] ids)
                {
                        return base.Get<long, ErrorDatum> ("Id", ids);
                }
        }
}
